/*!
 * @file modeConsole.cpp
 * @brief Ciało modeConsole.hh
 */

#include "modes/console/modeConsole.hh"
using namespace std;

/*--------------------------------------------------------------------------*/

modeConsole::modeConsole() :
	modeBase(MA_OFF, MK_IMPORTANT, MD_FOREGROUND, "console") {

	this->linesToIgnore = 0;

	cmd::setSStream(&this->consoleStream);
}

/*--------------------------------------------------------------------------*/

void modeConsole::init(){

}

/*--------------------------------------------------------------------------*/

modeConsole::~modeConsole() {

	ofstream file("console_dump");
	if (!file.fail()) {
		file << this->consoleContent;
		file.close();
	}

}

/*--------------------------------------------------------------------------*/

void modeConsole::render3d() {

}

/*--------------------------------------------------------------------------*/

void modeConsole::render2d() {

	//tło
	pawirr::graphic->_pri->putRectangle(point2f(0, 370), point2f(
			pawirr::graphic->resX, 0), color(0.3, 0.3, 0.3, 0.8));

	//zlicznie linii - żeby potem wyświetlać "końcówkę"
	int p = 0;
	int lines = 1;
	while (this->consoleContent[p] != '\0') {
		if (this->consoleContent[p] == '\n') {
			lines++;
		}
		p++;
	}

	if (lines <= 19) {
		this->linesToIgnore = -1;
	}

	if (this->linesToIgnore > lines - 19)
		this->linesToIgnore = lines - 19;

	int i = 0;
	int lines_behind = 0;
	int pos = 20;

	pawirr::graphic->_pri->putRectangle(
			point2f(pawirr::graphic->resX - 20, 10),
			point2f(pawirr::graphic->resX - 10, 340), color(0.95, 0.95, 0.95));

	if (lines <= 18) {
		pawirr::graphic->_pri->putRectangle(point2f(pawirr::graphic->resX - 19, 11), point2f(
				pawirr::graphic->resX - 11, 339), color(0.05, 0.05, 0.05));
	} else {
		float L = lines - 1; //całosć
		float T = lines - this->linesToIgnore - 19; //pole nad
		float B = 17; //cały pasek

		float pw = 328; //wysokość całego paska

		int top = 11 + T * pw / L;
		int bottom = top + ((B) * pw / L);

		if (bottom < 338)
			bottom = 339;

		pawirr::graphic->_pri->putRectangle(point2f(pawirr::graphic->resX - 19,
				top), point2f(pawirr::graphic->resX - 11, bottom), color(0.05, 0.05, 0.05));
	}

	string newLine;

	//wyświetlanie historii
	while (this->consoleContent[i] != '\0') {
		if (this->consoleContent[i] != '\n') {
			newLine += this->consoleContent[i];
		} else {
			if ((lines - this->linesToIgnore) <= lines_behind + 19) {
				color cl(0.0, 1.0, 0.0);

				if (newLine.length() > 2 && newLine[0] == '#') {

					switch (newLine[1]) {
					case '1':
						cl = color_red;
						break;
					case '2':
						cl = color_green;
						break;
					case '3':
						cl = color_blue;
						break;
					case '4':
						cl = color_yellow;
						break;
					case '5':
						cl = color_pink;
						break;
					case '6':
						cl = color_cyan;
						break;
					case '7':
						cl = color_white;
						break;
					case '8':
						cl = color_black;
						break;
					}//end of switch

					newLine = newLine.substr(2, newLine.length());
				}//end of if

				pawirr::temp->fontDefault->renderToScreen(point2f(10, pos), cl,
						newLine);

				newLine = "";
				pos += 19;
			} else {
				newLine = "";
				lines_behind++;
			}
		}
		if (pos > 340)
			break;
		i++;
	}

	//current input
	pawirr::temp->fontDefault->renderToScreen(point2f(10, 350), color(1.0, 1.0,
			1.0), "> " + this->currentInput + "|");
}

/*--------------------------------------------------------------------------*/

void modeConsole::calculate() {
	this->consoleContent += this->consoleStream.str();
	this->consoleStream.str("");
}

/*--------------------------------------------------------------------------*/

void modeConsole::input() {

	if (pawirr::keyboard->isPressed(k_pageup)) {
		this->linesToIgnore++;
	}
	if (pawirr::keyboard->isPressed(k_pagedown)) {
		if (this->linesToIgnore >= 0)
			this->linesToIgnore--;
	}

	if (pawirr::keyboard->isPressed(k_lshift)) {
		this->currentInput += '_';
	}
	if (pawirr::keyboard->isPressed(k_lctrl)) {
		this->currentInput += '/';
	}
	if (pawirr::keyboard->isPressed(k_q)) {
		this->currentInput += 'q';
	}
	if (pawirr::keyboard->isPressed(k_w)) {
		this->currentInput += 'w';
	}
	if (pawirr::keyboard->isPressed(k_e)) {
		this->currentInput += 'e';
	}
	if (pawirr::keyboard->isPressed(k_r)) {
		this->currentInput += 'r';
	}
	if (pawirr::keyboard->isPressed(k_t)) {
		this->currentInput += 't';
	}
	if (pawirr::keyboard->isPressed(k_y)) {
		this->currentInput += 'y';
	}
	if (pawirr::keyboard->isPressed(k_u)) {
		this->currentInput += 'u';
	}
	if (pawirr::keyboard->isPressed(k_i)) {
		this->currentInput += 'i';
	}
	if (pawirr::keyboard->isPressed(k_o)) {
		this->currentInput += 'o';
	}
	if (pawirr::keyboard->isPressed(k_p)) {
		this->currentInput += 'p';
	}
	if (pawirr::keyboard->isPressed(k_a)) {
		this->currentInput += 'a';
	}
	if (pawirr::keyboard->isPressed(k_s)) {
		this->currentInput += 's';
	}
	if (pawirr::keyboard->isPressed(k_d)) {
		this->currentInput += 'd';
	}
	if (pawirr::keyboard->isPressed(k_f)) {
		this->currentInput += 'f';
	}
	if (pawirr::keyboard->isPressed(k_g)) {
		this->currentInput += 'g';
	}
	if (pawirr::keyboard->isPressed(k_h)) {
		this->currentInput += 'h';
	}
	if (pawirr::keyboard->isPressed(k_j)) {
		this->currentInput += 'j';
	}
	if (pawirr::keyboard->isPressed(k_k)) {
		this->currentInput += 'k';
	}
	if (pawirr::keyboard->isPressed(k_l)) {
		this->currentInput += 'l';
	}
	if (pawirr::keyboard->isPressed(k_z)) {
		this->currentInput += 'z';
	}
	if (pawirr::keyboard->isPressed(k_x)) {
		this->currentInput += 'x';
	}
	if (pawirr::keyboard->isPressed(k_c)) {
		this->currentInput += 'c';
	}
	if (pawirr::keyboard->isPressed(k_v)) {
		this->currentInput += 'v';
	}
	if (pawirr::keyboard->isPressed(k_b)) {
		this->currentInput += 'b';
	}
	if (pawirr::keyboard->isPressed(k_n)) {
		this->currentInput += 'n';
	}
	if (pawirr::keyboard->isPressed(k_m)) {
		this->currentInput += 'm';
	}
	if (pawirr::keyboard->isPressed(k_1)) {
		this->currentInput += '1';
	}
	if (pawirr::keyboard->isPressed(k_2)) {
		this->currentInput += '2';
	}
	if (pawirr::keyboard->isPressed(k_3)) {
		this->currentInput += '3';
	}
	if (pawirr::keyboard->isPressed(k_4)) {
		this->currentInput += '4';
	}
	if (pawirr::keyboard->isPressed(k_5)) {
		this->currentInput += '5';
	}
	if (pawirr::keyboard->isPressed(k_6)) {
		this->currentInput += '6';
	}
	if (pawirr::keyboard->isPressed(k_7)) {
		this->currentInput += '7';
	}
	if (pawirr::keyboard->isPressed(k_8)) {
		this->currentInput += '8';
	}
	if (pawirr::keyboard->isPressed(k_9)) {
		this->currentInput += '9';
	}
	if (pawirr::keyboard->isPressed(k_0)) {
		this->currentInput += '0';
	}

	if (pawirr::keyboard->isPressed(k_backspace)) {
		this->currentInput = this->currentInput.substr(0,
				this->currentInput.length() - 1);
	}

	if (pawirr::keyboard->isPressed(k_space)) {
		this->currentInput += ' ';
	}

	if (pawirr::keyboard->isPressed(k_enter)) {
		this->consoleContent += "#7> " + this->currentInput + "\n";

		cmd::execute(this->currentInput);

		this->currentInput = "";
	}
}

/*--------------------------------------------------------------------------*/
